<?php
/**
 * 异动管理
*/
apf_require_class("Bll_BLL");
apf_require_class('Bll_Hr_OutInterface');
apf_require_class('Bll_Flow_Info');
apf_require_class('Bll_Flow_OperateLog');
apf_require_class('Bll_Flow_ApplyRelation');
class Bll_Flow_Transfer extends BLL {
    public function get_transfer_by_flow($flow_id){
        $return = array();
        if(!$flow_id){
            return $return;
        }
        return $this->get_default_dao()->find_row(array('flow_id'=>$flow_id));
    }
    /**
     * 添加异动申请
     * @param unknown_type $params
     * @return boolean
     */
    public function add_transfer($params){
        $result = false;
        if(empty($params['user_id']) || empty($params['apply_user_id']) || empty($params['flow_type']) || empty($params['city_id'])){
            return $result;
        }
        //ceo
        $ceo_uid = APF::get_instance()->get_config('ceo_uid');
        //加入流程
        $fre = $this->add_flow($params);
        if(!$fre){
            return $result;
        }
        //添加转岗基本信息
        $bre = $this->add_transfer_info($params,$fre);
        if(!$bre){
            return $result;
        }
        //添加父级关系,当不是ceo时才处理
        if($ceo_uid != $params['apply_user_id']){
            $rre = $this->add_parent_relation($params['user_id'],$fre,$params['flow_stage']+1,false,$params['apply_user_id']);
            if(!$rre){
                return $result;
            }
        }

        $skip_flow = 0;
        //有跳步需要,再更新下flow表
        $update_flow = array();
        if($ceo_uid==$params['apply_user_id']){//如果申请人是ceo 跳过2步
            $skip_flow =2;
            $update_flow = array(
                    'flow_stage'=>$params['flow_type']==Const_Flow::FLOW_TYPE_MONEY ? Const_Flow::FLOW_MONEY_3 : Const_Flow::FLOW_TRANSFER_3,
                    'flow_opt'=>Const_Flow::PHASE_13,
                    'flow_role_list'=>Const_Flow::ROLE_HR,
                    );
        }elseif($rre==$params['apply_user_id']){//给自己的下属调任或换主管,跳一步
            $skip_flow =1;
            $update_flow = array(
                    'flow_stage'=>$params['flow_type']==Const_Flow::FLOW_TYPE_MONEY ? Const_Flow::FLOW_MONEY_2 : Const_Flow::FLOW_TRANSFER_2,
                    'flow_opt'=>Const_Flow::PHASE_12,
                    'flow_role_list'=>Const_Flow::ROLE_SUPERIOR,
            );
        }

        if(!empty($update_flow)){
            $bll_flow = new Bll_Flow_Info();
            $bll_flow->update_flow_data($update_flow,array('flow_id'=>$fre));
        }

        //计入操作日志
        $lre = $this->add_opt_log($params,$fre,$skip_flow);
        if(!$lre){
            return $result;
        }
        return true;
    }
    /**
     * 更新每一步操作的转岗表
     * @param unknown_type $flow_id
     * @param unknown_type $params
     * @return boolean
     */
    public function update_opt($flow_id,$params){
        $return = false;
        if(empty($flow_id) && empty($params)){
            return $return;
        }
        if($params['effective_date']){
            //更新日志表
            $bll_log = new Bll_Flow_OperateLog();
            $lre = $bll_log->update_plan_date($flow_id,intval($params['effective_date']));
        }

        return $this->get_default_dao()->update_by_id($flow_id,$params);
    }
    /**
     * 更新转岗基本信息
     * @param unknown_type $params
     */
    public function update_transfer($params){
        $flow_id = intval($params['flow_id']);
        if(!$flow_id){
            return false;
        }
        $plan_date = intval($params['plan_date']);
        $data = array(
            'flow_type' => intval($params['flow_type']),
            'salary_type' => intval($params['salary_type']),
            'salary_reason' => trim($params['salary_reason']),
            'transfer_type' => intval($params['transfer_type']),
            'transfer_reason' => trim($params['transfer_reason']),
            'after_info' => trim($params['job_name']),
            'new_job_id' => intval($params['job_id']),
            'new_city_id' => isset($params['new_city_id']) ? intval($params['new_city_id']) : 0,//新职位城市
            'new_super_uid' => isset($params['new_super_uid']) ? intval($params['new_super_uid']) : 0,//新主管id
            'super_name' => isset($params['super_name']) ? trim($params['super_name']) : '',//新主管id职位的信息
            'p_level' => intval($params['p_level']),
            'hr_p_level' => intval($params['p_level']),//顺便设置HR确认level的默认值
            'hr_m_level' => intval($params['m_level']),//顺便设置HR确认level的默认值
            'hrd_p_level' => intval($params['p_level']),//顺便设置HRD确认level的默认值
            'hrd_m_level' => intval($params['m_level']),//顺便设置HRD确认level的默认值
            'm_level' => intval($params['m_level']),
            'plan_date' => $plan_date,
            'effective_date'=> intval($params['plan_date']),//主管确认时间，默认写成计划时间
        );

        //更新流程表生效时间
        $bll_flow = new Bll_Flow_Info();
        //更新流程类型和生效时间
        $fre = $bll_flow->update_flow_entry($flow_id,array('effect_date' => $plan_date,'flow_type'=>intval($params['flow_type'])));
        //更新日志表
        $bll_log = new Bll_Flow_OperateLog();
        $lre = $bll_log->update_plan_date($flow_id,$plan_date);
        if($fre && $lre){
            return $this->get_default_dao()->update_by_id($flow_id,$data);
        }else{
            return false;
        }
    }
    /**
     * 添加记录到流程表
     * @param unknown_type $params
     * @return boolean
     */
    private function add_flow($params){
        //先加入流程表
        $bll_flow = new Bll_Flow_Info();
        $data = array(
                'user_id'       => intval($params['user_id']),
                'user_name'     => trim($params['user_name']),
                'flow_type'     => intval($params['flow_type']),
                'effect_date'   => intval($params['plan_date']),
                'flow_stage'    => intval($params['flow_stage']),
                'flow_opt'      => intval($params['flow_opt']),
                'work_city_id'  => intval($params['city_id']),
                'flow_role_list' => $params['flow_role_list'],
                'now_status'    => 1,
        );
        return $bll_flow -> insert_flow($data);
    }
    /**
     * 构造被转岗前信息
     * @param unknown_type $params
     * @return string
     */
    private function build_before_info($user_info){
        $p_level='';
        $m_level='';
        if($user_info['p_level']) {
            $p_level = 'P'.(($user_info['p_level']==99)? 0 :$user_info['p_level']);
        }
        if($user_info['m_level']){
            $m_level= 'M'.(($user_info['m_level']==99)? 0 :$user_info['m_level']);
        }
        $before_change = "{$user_info['user_name']},{$user_info['user_code']},{$user_info['city_name']},{$user_info['bu_name']},{$user_info['department_name']},{$user_info['position_name']},{$user_info['enter_date']}";
        $level = $p_level.' '.$m_level;
        $before_change .= ",$level";
        return $before_change;
    }
    /**
     * 添加转岗基本表数据
     * @param unknown_type $params
     * @param unknown_type $flow_id
     */
    private function add_transfer_info($params, $flow_id){
        //加入转岗基础表
        $bll_hr = new Bll_Hr_OutInterface();
        $user_info = $bll_hr->get_user_base_info(intval($params['user_id']),1);
        if(!$user_info){
            return false;
        }
        $before_change = $this->build_before_info($user_info);
        $data = array(
                'apply_user_id' => intval($params['apply_user_id']),
                'user_id'       => intval($params['user_id']),
                'flow_type'     => intval($params['flow_type']),
                'old_job_id'    => intval($user_info['job_id']),
                'flow_id'       => intval($flow_id),
                'before_info'   => $before_change,
                'plan_date' => intval($params['plan_date']),
                'effective_date'=> intval($params['plan_date']),//主管确认时间，默认写成计划时间
        );
        $data['transfer_type'] = isset($params['transfer_type']) ? intval($params['transfer_type']) : 0;//转岗原因类型
        $data['transfer_reason'] = isset($params['transfer_reason']) ? trim($params['transfer_reason']) : '';//转岗原因备注
        $data['new_job_id'] = isset($params['job_id']) ? intval($params['job_id']) : 0;//申请JOBid
        $data['after_info'] = isset($params['job_name']) ? trim($params['job_name']) : '';//申请职位的信息
        //可变更主管
        $data['new_city_id'] = isset($params['new_city_id']) ? intval($params['new_city_id']) : 0;//新职位城市
        $data['new_super_uid'] = isset($params['new_super_uid']) ? intval($params['new_super_uid']) : 0;//新主管id
        $data['super_name'] = isset($params['super_name']) ? trim($params['super_name']) : '';//新主管id职位的信息

        $data['salary_type'] = isset($params['salary_type']) ? intval($params['salary_type']) : 0;//薪资异动原因
        $data['salary_reason'] = isset($params['salary_reason']) ? trim($params['salary_reason']) : '';//薪资异动原因备注
        $data['plan_date'] = isset($params['plan_date']) ? intval($params['plan_date']) : 0;
        $data['p_level'] = isset($params['p_level']) ? intval($params['p_level']) : 0;//申请P级
        $data['m_level'] = isset($params['m_level']) ? intval($params['m_level']) : 0;//申请M级
        $data['hr_p_level'] = isset($params['p_level']) ? intval($params['p_level']) : 0;//顺便设置HR确认level的默认值
        $data['hr_m_level'] = isset($params['m_level']) ? intval($params['m_level']) : 0;//顺便设置HR确认level的默认值
        $data['hrd_p_level'] = isset($params['p_level']) ? intval($params['p_level']) : 0;//顺便设置HRD确认level的默认值
        $data['hrd_m_level'] = isset($params['m_level']) ? intval($params['m_level']) : 0;//顺便设置HRD确认level的默认值

        return $this->get_default_dao()->insert($data);
    }
    /**
     * 添加主管关系，主管的主管关系
     * @param unknown_type $params
     * @param unknown_type $flow_id
     * @return boolean
     */
    public function add_parent_relation($user_id,$flow_id,$flow_stage,$double=false,$apply_user_id=0){
        //添加父级关系
        $bll_hr = new Bll_Hr_OutInterface();
        $parent_info = $bll_hr->get_under_leader(intval($user_id));

        $super_id = 0;
        if(!empty($parent_info) && $apply_user_id && $apply_user_id==$parent_info['user_id']){//是给自己下属申请也不处理
            $double = true;
            $flow_stage +=1;//需要再进一个阶段
            $super_id = $parent_info['user_id'];
        }
        if($double){
            $super_id = $apply_user_id;//$parent_info['user_id'];
            $parent_info = $bll_hr->get_under_leader($apply_user_id);
        }
        if(!$parent_info){
            return false;
        }

        $bll_rela = new Bll_Flow_ApplyRelation();
        $bll_rela->insert($flow_id,intval($user_id),$parent_info['user_id'],$flow_stage);
        return $super_id ? $super_id : $parent_info['user_id'];
    }
   /**
    * 根据flow_id清理上级关系
    * @param unknown_type $flow_id
    * @return boolean
    */
    public function clear_parent_relation($flow_id){
        $return = false;
        if(!$flow_id){
            return $return;
        }
        $bll_re = new Bll_Flow_ApplyRelation();
        return $bll_re->update_by_flow($flow_id);
    }
    /**
     * 添加操作日志
     * @param unknown_type $params
     * @param unknown_type $flow_id
     * @param int $skip_flow 是否需要跳流程,0不跳 1为跳办理人主管 2为两级主管(ceo)
     */
    public function add_opt_log($params, $flow_id,$skip_flow=0){
        //添加操作日志
        $data = array(
                'user_id'       => intval($params['user_id']),
                'user_name'     => trim($params['user_name']),
                'opt_user_id'   => intval($params['apply_user_id']),
                'flow_id'       => intval($flow_id),
                'flow_type'     => intval($params['flow_type']),
                'effect_date'   => intval($params['effective_date'])? intval($params['effective_date']) : intval($params['plan_date']),
                'flow_stage'    => intval($params['flow_stage']),
                'flow_opt'      => intval($params['flow_opt']),
                'work_city_id'  => intval($params['city_id']),
                'role_type'     => $params['role_type'],
        );
        $bll_log = new Bll_Flow_OperateLog();

        $data_one = $data_two = $data;
        $data_one['flow_stage']= $params['flow_type']==Const_Flow::FLOW_TYPE_TRANSFER ? Const_Flow::FLOW_TRANSFER_2 : Const_Flow::FLOW_MONEY_2;
        $data_one['flow_opt']= Const_Flow::PHASE_12;

        $data_two['flow_stage'] = $params['flow_type']==Const_Flow::FLOW_TYPE_TRANSFER ? Const_Flow::FLOW_TRANSFER_3 : Const_Flow::FLOW_MONEY_3;
        $data_two['flow_opt']= Const_Flow::PHASE_13;

        $list = array($data,$data_one,$data_two);
        foreach($list as $k=>$val){
            if($k<=$skip_flow){
                $bll_log->save_log($val);
            }
        }
        return true;
    }

    /**
     *
     * @param unknown_type $flow_ids
     * @return multitype:
     */
    public function find_by_ids($flow_ids,$is_short=1){
        $data = array();
        if(empty($flow_ids)){
            return $data;
        }
        $_where["flow_id IN(".implode(',',$flow_ids).") AND 1=?"]=1;
        $field = array();
        if($is_short){
            $field = array('flow_id','user_id','old_job_id');
        }
        return $this->get_default_dao()->find_short_assoc($_where, "", 0,0,$field);
    }

    private function get_default_dao ()
    {
        $dao = $this->get_dao('Dao_Flow_Transfer');
        return $dao;
    }
}
